package com.ktjy.crm.service.impl;

import com.ktjy.crm.entity.User;
import com.ktjy.crm.mapper.UserMapper;
import com.ktjy.crm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User saveOrUpdate(User user, String usrName) {
        if (user.getUsrId() == null) {
            throw new IllegalArgumentException("用户ID不能为空，禁止全表更新");
        }
        // 实际保存或更新逻辑
        if(user.getUsrId() == null) {
            userMapper.insert(user);
        } else {
            userMapper.updateById(user);
        }
        return user;
    }

    @Override
    public int delete(Long id) {
        if (id == null || id <= 0) {  // 添加ID有效性检查
            throw new IllegalArgumentException("删除ID必须为正数");
        }
        // 可以添加业务逻辑检查，如检查用户是否存在等
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new RuntimeException("要删除的用户不存在");
        }
        return userMapper.deleteById(id);
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }
}
